﻿using Coldairarrow.Business.IT;
using Coldairarrow.Entity.IT;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Coldairarrow.IBusiness;
using Coldairarrow.Entity.DTO;
using System;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

namespace Coldairarrow.Api.Controllers.IT
{
    [Route("/IT/[controller]/[action]")]
    public class IT_LocalDetailController : BaseApiController
    {
        #region DI

        public IT_LocalDetailController(IIT_LocalDetailBusiness iT_LocalDetailBus,IOperator op)
        {
            _iT_LocalDetailBus = iT_LocalDetailBus;
            _Op = op;
        }
        IOperator _Op { get; }
        IIT_LocalDetailBusiness _iT_LocalDetailBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<IT_LocalDetail>> GetDataList(IT_LocalDetailPageInput input)
        {
            input.StorId = _Op.Property.DefaultStorageId;
            return await _iT_LocalDetailBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<IT_LocalDetail> GetTheData(IdInputDTO input)
        {
            return await _iT_LocalDetailBus.GetTheDataAsync(input.id);
        }
        /// <summary>
        /// 库存明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<IT_LocalDetailDTO>> GetDataListAsyncDtoList(PageInput<IT_LocalDetailConditionDTO> input) {
            return await _iT_LocalDetailBus.GetDataListAsyncDtoList(input);
        }
        /// <summary>
        /// 库存明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<IT_LocalDetailDtolist>> IT_LocalDetailList(PageInput<IT_LocalDetailDtolistContionDTO> input)
        {
            return await _iT_LocalDetailBus.IT_LocalDetailList(input);
        }
        /// <summary>
        /// 获取库存明细下的包信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<IT_LocalDetailDTO>> GetWarehouseList(PageInput<IT_LocalDetailConditionDTO> input)
        {
            return await _iT_LocalDetailBus.GetWarehouseList(input);
        }

       
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(IT_LocalDetail data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _iT_LocalDetailBus.AddDataAsync(data);
            }
            else
            {
                await _iT_LocalDetailBus.UpdateDataAsync(data);
            }
        }


        /// <summary>
        /// /获取汇总数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<IT_LocalDetailDtolist>> GetDataListAsynclist()
        {
            var res= _iT_LocalDetailBus.GetDataListAsynclist();
            return await res;
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _iT_LocalDetailBus.DeleteDataAsync(ids);
        }

        /// <summary>
        ///导出库存明细接口  通用  朱庆龙     2022/3/22
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public  async Task <IActionResult> GetExportList(PageInput<IT_LocalDetailDtolistContionDTO> input)
        {
           var res =await  _iT_LocalDetailBus.IT_LocalDetailList(input);
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet s1 = book.CreateSheet("可用库存");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("仓库");
            r1.CreateCell(1).SetCellValue("库区/货架/货位");
            r1.CreateCell(2).SetCellValue("物料编码");
            r1.CreateCell(3).SetCellValue("物料名称");
            r1.CreateCell(4).SetCellValue("规格");
            r1.CreateCell(5).SetCellValue("单位");
            r1.CreateCell(6).SetCellValue("库存数量");
            r1.CreateCell(7).SetCellValue("单重");
            r1.CreateCell(8).SetCellValue("总重");
            for (int i = 0; i < res.Data.Count; i++)
            {
                IRow rt = s1.CreateRow(i + 1);
                rt.CreateCell(0).SetCellValue(res.Data[i].StorIdName);
                rt.CreateCell(1).SetCellValue(res.Data[i].StoareaName + "/" + res.Data[i].RackName + "/" + res.Data[i].LocalIdName);
                rt.CreateCell(2).SetCellValue(res.Data[i].materialCode);
                rt.CreateCell(3).SetCellValue(res.Data[i].MaterialIdName);
                rt.CreateCell(4).SetCellValue(res.Data[i].MaterSpec);
                rt.CreateCell(5).SetCellValue(res.Data[i].UnitemName);
                rt.CreateCell(6).SetCellValue(Convert.ToDouble(res.Data[i].Num.ToString()));
                rt.CreateCell(7).SetCellValue(Convert.ToDouble(res.Data[i].Weight));
                rt.CreateCell(8).SetCellValue(Convert.ToDouble(res.Data[i].TotalWeight));
            }
            //输出的文件名称
            string fileName = "可用库存" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            book.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);



        }
        #endregion
    }
}